<style scoped>
  .el-input--medium .el-input__inner {
    border: 0;
  }
</style>
<template>
  <div class="dashboard-editor-container" :style="{backgroundImage: 'url(' + bgImg + ')' }">
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="off" label-position="left">
        <div class="title-container">
          <h3 class="title">找回密码</h3>
        </div>
        <!--<el-form-item prop="userName">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
          <el-input v-model="loginForm.userName" name="username" type="text" auto-complete="on" placeholder="用户名" />
        </el-form-item>-->
        <el-form-item prop="mobile">
          <div style="display: flex">
            <span class="svg-container svg-container_login" style="font-size: 22px;padding-left: 11px;">
              <svg-icon icon-class="mobile" />
            </span>
            <el-input v-model="loginForm.mobile" name="mobile" auto-complete="on" placeholder="手机号" @keyup.enter.native="handleLogin" />
            <el-button :loading="phoneloading" type="primary" :disabled="phoneDisabled" style="width: 130px"
                       @click.stop="handlesend">{{phoneText}}
            </el-button>
          </div>
        </el-form-item>
        <el-form-item prop="checkSms">
          <span class="svg-container">
            <i class="el-icon-warning" style="font-size: 18px"/>
          </span>
          <el-input type="text" v-model="loginForm.checkSms" name="checkpic" autocomplete="off" placeholder="短信验证码" @keyup.enter.native="handleLogin" />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :type="passwordType" v-model="loginForm.password" name="password" auto-complete="new-password" placeholder="新密码" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <el-form-item prop="confirmPwd">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :type="passwordType" v-model="loginForm.confirmPwd" name="password" autocomplete="off" placeholder="确认密码" @keyup.enter.native="handleLogin" />
          <!--<span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye" />
          </span>-->
        </el-form-item>
        <el-form-item style="border:0;padding-bottom:25px;background: none">
          <span class="show-forget">
            <router-link to="/login/index">记起密码？</router-link>
          </span>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">提交</el-button>

      </el-form>

    </div>
    <footercopy/>
    <!--<div class="index-footer">
      <div class="div-footer">
        <p class="dialog-font">
          <span>Copyright&nbsp;&copy;&nbsp;&nbsp;{{ date }}&nbsp;&nbsp;享和宁杰金融信息服务（上海）有限公司&nbsp;&nbsp;版权所有&nbsp;&nbsp;</span>
          <a href="http://www.beian.miit.gov.cn/" target="_blank">沪ICP备19047252号-2</a>
        </p>
      </div>
    </div>-->
  </div>
</template>

<script>
import { doGet } from '@/api/index'
import bgImg from '../../assets/xh-bg.jpg'
import Footercopy from '../company/footercopy'

export default {
  name: 'ResetPassword',
  components: {
    Footercopy,
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度应大于6'))
      } else {
        callback()
      }
    }
    const validatePassword2 = (rule, value, callback) => {
      if (value !== this.loginForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validateCheckSms = (rule, value, callback) => {
      if (value.length != 6) {
        callback(new Error('短信验证码必须为6位'))
      } else {
        callback()
      }
    }
    let mobileValidate = (rule, value, callback) => {
      let reg = /^[1][0-9]{10}$/;
      if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'));
      } else {
        callback()
      }
    }
    return {
      date: '',
      bgImg: bgImg,
      checkpicSRC: '',
      loginForm: {
        userName: '',
        password: '',
        checkSms: '',
        mobile: '',
        confirmPwd: ''
      },
      loginRules: {
        userName: [{ required: true, message: '账户不允许为空', trigger: 'blur' }],
        password: [
          { required: true, message: '新密码不允许为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: ' 确认密码不能为空', trigger: 'blur' },
          { validator: validatePassword2, trigger: 'blur' }
        ],
        checkSms: [
          { required: true, message: '短信验证码不允许为空', trigger: 'blur' },
          { validator: validateCheckSms, trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '手机号不允许为空', trigger: 'blur' },
          { validator: mobileValidate, trigger: 'blur' }],
      },
      phoneloading: false,
      passwordType: 'password',
      loading: false,
      phoneDisabled: true,
      timer: null,
      phoneText: '获取验证码'
    }
  },
  watch: {
    loginForm: {
      handler: function(val) {
        let reg = /^[1][0-9]{10}$/;
        if (!reg.test(val.mobile)) {
          this.phoneDisabled = true
        } else {
          if (!this.timer) {
            this.phoneDisabled = false
          }
        }
      },
      deep: true
    }
  },
  created() {
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
    clearTimeout(this.timer)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          doGet('login.html?op=resetpwd', this.loginForm).then((response) => {
            this.loading = false
            this.$router.back()
          }).catch((error) => {
            this.$notify.error({
              title: '失败',
              message: error.message
            })
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    handlesend() {
      this.phoneloading = true
      doGet("sendsms.html",{phone: this.loginForm.mobile}).then((response)=>{
        this.phoneDisabled = true
        this.phoneloading = false
        this.setTime(300)
      }).catch((error)=>{
        this.$notify.error({
          title: '失败',
          message: error.message
        })
        this.phoneloading = false
      })
    },

    setTime(time) {
      if (time <= 0) {
        this.phoneText = '重新获取'
        this.phoneDisabled = false
        clearTimeout(this.timer)
        return
      }
      this.phoneText = `还剩${time}s`
      this.timer = setTimeout(() => {
        time--
        this.setTime(time)
      }, 1000)
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      /*&:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }*/
    }
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.dashboard-editor-container {
  padding: 20px 0px 0px;
  background-color: #f7f7f7;
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}
.login-container {
  /*position: fixed;*/
  height: 670px;
  width: 100%;
  /*background-color: $bg;*/
  /*overflow: auto;*/
  min-width: 1200px;
  max-width: 1920px;
  margin: 0 auto;
  /*padding-top: 80px;*/
  /*background: url(../../assets/ec62c7cf.png) top no-repeat;
  background-size: cover;*/
  .el-form-item {
    border: 1px solid #ccc;
    /*background: rgba(0, 0, 0, 0.1);*/
    border-radius: 5px;
    color: #454545;
  }
  .login-form {
    /*position: absolute;*/
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
    background: #fff;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      /*color: $light_gray;*/
      color:#333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd ,.show-forget {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .show-forget {
    &:hover {
      color: #cccccc;
      border-bottom: 1px solid #cccccc;
    }
  }
}
</style>
